package us.mooresr.webtroopmanager.client.activity;

import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.events.RoleAddedEvent;
import us.mooresr.webtroopmanager.client.events.RoleAddedHandler;
import us.mooresr.webtroopmanager.client.events.RoleDeletedEvent;
import us.mooresr.webtroopmanager.client.events.RoleDeletedHandler;
import us.mooresr.webtroopmanager.client.ui.RoleListView;
import us.mooresr.webtroopmanager.shared.Role;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class RoleListActivity extends MyActivity implements RoleListView.Presenter {

	public RoleListActivity(ClientFactory clientFactory) {
		super(clientFactory);
	}
	
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		RoleListView roleListView = clientFactory.getRoleListView();
		roleListView.setPresenter(this);
		panel.setWidget(roleListView.asWidget());
		
		loadData();
		
		eventBus.addHandler(RoleDeletedEvent.TYPE,new RoleDeletedHandler() {
			
			@Override
			public void onDeleteEvent(RoleDeletedEvent event) {
				RoleListView roleListView = clientFactory.getRoleListView();
				roleListView.removeRole(event.getDeletedId());
			}
		});
		
		eventBus.addHandler(RoleAddedEvent.TYPE, new RoleAddedHandler() {

			@Override
			public void onAddEvent(RoleAddedEvent event) {
				RoleListView roleListView = clientFactory.getRoleListView();
				roleListView.addRole(event.getRole());
				
			}
			
		});
	}

	@Override
	public void deleteRole(Role role) {
		Long key = role.getId();
		
		if (Window.confirm("Delete role " + role.toString() + "?")) {
			TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
			
			
			AsyncCallback<Long> callback = new AsyncCallback<Long>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(Long result) {
					
					clientFactory.getEventBus().fireEvent(new RoleDeletedEvent(result));
				}
			};
			
			troopManagerService.deleteRole(key, callback);
		}	
		
		
	}
	
	private void loadData() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		
		AsyncCallback<List<Role>> callback = new AsyncCallback<List<Role>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<Role> result) {
				
				clientFactory.getRoleListView().setData(result);
				clientFactory.getRoleListView().setStatus("Ready.");
			}
		};
		clientFactory.getRoleListView().setStatus("Retrieving data...");
		troopManagerService.getAllRoles(callback);
	}

}
